<?

/**
 * Provides manage your locales record, and localization for your application.
 * 
 * @version since before begin. :)
 * @author asmhack
 * @license GPL
 * @copyright Serhiy Zaporozhets'
 */
class Localizator {
   
   private $locale = null;
   
   private $localesArray;
   
   private $file = null;
   
   private $localeDir;
   
   private function __construct() {
      $this->localesArray = array ();
      $this->locale = null;
      $this->getListOfLocales ();
      $this->getLocalizeDir ();
      $this->findValidLocalizeFile ();
   }
   
   public static function getInstance() {
      if (! Session::isExist ( 'i18n', false ))
         Session::putSystemValue ( 'i18n', new Localizator ( ) );
      return Session::getSystemValue ( 'i18n' );
   }
   
   private function __clone() {
   }
   
   public function getListOfLocales() {
      if (isset ( $_SERVER ['HTTP_ACCEPT_LANGUAGE'] )) {
         $loc = $_SERVER ['HTTP_ACCEPT_LANGUAGE'];
         $parts = explode ( ';', $loc );
         
         $l = explode ( ',', $parts [0] );
         
         foreach ( $l as $v )
            $this->localesArray [] = $v;
         sort ( $this->localesArray );
      }
   }
   
   private function getLocalizeDir() {
      $this->localeDir = Session::getSystemValue ( 'localization', false );
   }
   
   private function findValidLocalizeFile() {
      
      $handle = @opendir ( $this->localeDir );
      $files = scandir ( $this->localeDir );
      $flag = false;
      foreach ( $this->localesArray as $locale ) {
         if (! $flag)
            foreach ( $files as $file ) {
               if (! is_dir ( $file )) {
                  $loc = explode ( '.', $file );
                  //if (stristr($locale, $loc [0])) {
                  if ($loc [0] == $locale) {
                     $this->locale = $locale [0];
                     $this->file = $file;
                     $flag = true;
                     break;
                  }
               }
            }
      }
      closedir ( $handle );
      
      if ($this->locale == null) // if no one locale choosed:: then set default if it set in config.php
         
         if (Session::isExist ( 'defaultLocale', false )) {
            $this->locale = Session::getSystemValue ( 'defaultLocale', false );
            $this->file = $this->locale . '.php'; //properties
            if (! file_exists ( $this->localeDir . $this->file ))
               throw new LocalizatorException ( 'Language file <b>'.$this->localeDir . $this->file.'</b> not found! ' . $this->localeDir . $this->file );
         } else 
            $this->locale ='en';
   }
   
   private function selfUpdate() {
      Session::putSystemValue ( 'i18n', $this );
   }
   
   function getMessage($key) {
      if (! file_exists ( $this->localeDir . $this->file ))
         throw new LocalizatorException ( 'Language file <b>' . $this->localeDir . $this->file . '</b>  not found! Can\'t read key <b>' . $key . '</b>' );
      global $lang;
      ob_start ();
      @include_once ($this->localeDir . $this->file);
      $ret = ob_get_contents ();
      ob_end_clean ();
      chdir ( dirname ( $_SERVER ['SCRIPT_FILENAME'] ) );
      if (! array_key_exists ( $key, $lang ))
         throw new LocalizatorException ( 'Can\'t find key <b>' . $key . '</b> in <b>' . $this->file . '</b> file.' );
      return $lang [$key];
   }
}
?>